const fs = require("fs");
const userService = require("../service/user.service");
const fileService = require("../service/file.service");
const { AVATAR_PATH } = require("../constans/file-path");
const { logger } = require("../../logger");
const User = require("../model/user.model");
const Apply = require("../model/apply.model");
const Question = require("../model/question.model");
const { Op } = require("sequelize");
const { authParams, authParamsBody } = require("../utils/auth-params");

class UserController {
  // 创建用户
  async create(ctx, next) {
    await User.create(ctx.request.body)
      .then((res) => {
        ctx.success(res);
      })
      .catch((err) => {
        ctx.fail();
        logger.error(error);
      });
  }

  async update(ctx, next) {
    const { id } = ctx.request.body;
    await User.update(ctx.request.body, {
      where: {
        id,
      },
    })
      .then((res) => {
        res[0] ? ctx.success() : ctx.fail();
      })
      .catch((err) => {
        ctx.fail();
        logger.error(err);
      });
  }

  async list(ctx, next) {
    const { limit, offset, keyword, permission } = ctx.query;
    authParams(ctx, limit, offset);
    await User.findAndCountAll({
      where: {
        username: {
          [Op.like]: `%${keyword}%`,
        },
        permission,
      },
      include: [
        {
          model: Question,
        },
      ],
      // 分页受include影响的问题
      distinct: true,
      limit: parseInt(limit),
      offset: parseInt(limit) * parseInt(offset - 1),
    })
      .then((res) => {
        ctx.success({
          list: res.rows,
          total: res.count,
          limit,
          offset,
        });
      })
      .catch((err) => {
        ctx.fail();
        logger.error(err);
      });
  }

  async detail(ctx, next) {
    const { userId } = ctx.params;
    await User.findOne({
      where: { id: userId },
      include: [
        {
          model: Question,
        },
        {
          model: Apply,
        },
      ],
    })
      .then((res) => {
        ctx.success(res);
      })
      .catch((err) => {
        ctx.fail(99);
        logger.error(err);
      });
  }

  // 修改注册状态
  async registerStatus(ctx, next) {
    const { status, id } = ctx.request.body;
    authParams(ctx, id);
    await User.update(
      { register_status: status },
      {
        where: { id },
        raw: true,
      }
    )
      .then((res) => {
        ctx.success();
      })
      .catch((err) => {
        ctx.fail(99);
        logger.error(err);
      });
  }

  // 修改专家类型
  async expertType(ctx, next) {
    const { type, id } = ctx.request.body;
    authParams(ctx, id);
    await User.update(
      { expert_type: type },
      {
        where: { id },
        raw: true,
      }
    )
      .then((res) => {
        ctx.success();
      })
      .catch((err) => {
        ctx.fail(99);
        logger.error(err);
      });
  }

  // 创建用户头像
  // async avatarInfo(ctx, next) {
  //   // 1.用户的头像是哪一个文件呢？
  //   const { userId } = ctx.params;
  //   const avatarInfo = await fileService.getAvatarByUserId(userId);

  //   // 2.提供图像信息
  //   // ctx.body = fs.createReadStream(`${AVATAR_PATH}/${avatarInfo.filename}`) //这样可以直接下载文件
  //   ctx.response.set("content-type", avatarInfo.mimetype);
  //   ctx.body = fs.createReadStream(`${AVATAR_PATH}/${avatarInfo.filename}`);
  // }
}

module.exports = new UserController();
